package com.ouheng.shop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ouheng.common.domain.Tree;
import com.ouheng.common.utils.BuildTree;
import com.ouheng.common.utils.StringUtils;
import com.ouheng.shop.dao.AxImageDao;
import com.ouheng.shop.dao.ProductColorDao;
import com.ouheng.shop.dao.ProductDao;
import com.ouheng.shop.dao.ProductPriceDao;
import com.ouheng.shop.dao.ProductProductGroupDao;
import com.ouheng.shop.dao.ProductPropertyDao;
import com.ouheng.shop.dao.ProductRelevanceDao;
import com.ouheng.shop.dao.ProductSizeDao;
import com.ouheng.shop.domain.AxImageDO;
import com.ouheng.shop.domain.ProductColorDO;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.domain.ProductPropertyDO;
import com.ouheng.shop.domain.ProductRegionDO;
import com.ouheng.shop.domain.ProductRelevanceDO;
import com.ouheng.shop.domain.ProductSizeDO;
import com.ouheng.shop.service.ProductService;



@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private ProductPropertyDao productPropertyDao;
	
	@Autowired
	private ProductPriceDao productPriceDao;
	
	@Autowired
	private ProductProductGroupDao productProductGroupDao;
	
	@Autowired
	private AxImageDao axImageDao;
	
	@Autowired
	private ProductRelevanceDao productRelevanceDao;
	
	@Autowired
	private ProductSizeDao productSizeDao;
	
	@Autowired
	private ProductColorDao productColorDao;
	
	@Override
	public ProductDO get(Long id){
		return productDao.get(id);
	}
	
	@Override
	public List<ProductDO> list(Map<String, Object> map){
		return productDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return productDao.count(map);
	}
	
	@Override
	@Transactional
	public int update(ProductDO product){
		product.setModifydate(new Date());
		return productDao.update(product);
	}
	
	@Override
	@Transactional
	public int remove(Long id){
		productPropertyDao.removeByPid(id);
		productPriceDao.removeByPid(id);
		productProductGroupDao.removeByProductid(id);
		productSizeDao.removeByPid(id);
		productColorDao.removeByPid(id);
		productRelevanceDao.removeByProductid(id);
		Map<String, Object> map = new HashMap<>();
		map.put("pkid", id);
		map.put("type", "1");
		axImageDao.removeByPkIdAndType(map);
		return productDao.remove(id);
	}
	
	@Override
	@Transactional
	public int batchRemove(Long[] ids){
		for (Long long1 : ids) {
			productPropertyDao.removeByPid(long1);
			productPriceDao.removeByPid(long1);
			productProductGroupDao.removeByProductid(long1);
			productSizeDao.removeByPid(long1);
			productColorDao.removeByPid(long1);
			productRelevanceDao.removeByProductid(long1);
			Map<String, Object> map = new HashMap<>();
			map.put("pkid", long1);
			map.put("type", "1");
			axImageDao.removeByPkIdAndType(map);
		}
		return productDao.batchRemove(ids);
	}

	@Override
	public int saveImage(ProductDO product, List<Object> list, List<Object> list1) {
		Map<String, Object> map = new HashMap<>();
		map.put("pkid", product.getId());
		map.put("type", 1);
		axImageDao.removeByPkIdAndType(map);
		Map<String, Object> map1 = new HashMap<>();
		map1.put("pkid", product.getId());
		map1.put("type", 3);
		axImageDao.removeByPkIdAndType(map1);
		int sum = productDao.update(product);
		if (!list.isEmpty()) {
            for (Object obj : list) {
                if (obj != null) {
                	AxImageDO axImageDO = (AxImageDO) obj;
                    if (com.ouheng.common.utils.StringUtils.isNoneBlank(axImageDO.getSrc())) {
                    	axImageDO.setPkid(product.getId());
                    	axImageDO.setSrc(axImageDO.getSrc());
                    	axImageDO.setType("1");
                    	axImageDO.setCreatedate(new Date());
                    	axImageDao.save(axImageDO);
                    }
                }
            }
        }
		if (!list1.isEmpty()) {
            for (Object obj : list1) {
                if (obj != null) {
                	AxImageDO axImageDO = (AxImageDO) obj;
                    if (com.ouheng.common.utils.StringUtils.isNoneBlank(axImageDO.getSrc())) {
                    	axImageDO.setPkid(product.getId());
                    	axImageDO.setSrc(axImageDO.getSrc());
                    	axImageDO.setType("3");
                    	axImageDO.setCreatedate(new Date());
                    	axImageDao.save(axImageDO);
                    }
                }
            }
        }
		return sum;
	}

	@Override
	public Tree<ProductDO> getTree(Long id) {
		List<ProductDO> ids = productDao.list(new HashMap<String, Object>(16));
		List<Long> ppids = productRelevanceDao.listPPidByProductid(id);
		List<Long> temp = ppids;
//		for (ProductDO regionDO : ids) {
//			if (temp.contains(regionDO.getId())) {
//				ppids.remove(regionDO.getId());
//			}
//		}
		
		List<Tree<ProductDO>> trees = new ArrayList<Tree<ProductDO>>();
		List<ProductDO> regionDOs = productDao.list(new HashMap<String, Object>(16));
		for (ProductDO regionDO : regionDOs) {
			Tree<ProductDO> tree = new Tree<ProductDO>();
			tree.setId(regionDO.getId().toString());
			tree.setParentId(null);
			tree.setText(regionDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = regionDO.getId();
			if (ppids.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ProductDO> t = BuildTree.build(trees);
		return t;
	}

//	@Override
//	@Transactional
//	public void saveRelevance(ProductDO product) {
//		productRelevanceDao.removeByProductid(product.getId());
//		List<Long> ppids = product.getPpids();
//		for (Long long1 : ppids) {
//			ProductRelevanceDO productRelevanceDO = new ProductRelevanceDO();
//			productRelevanceDO.setPid(product.getId());
//			productRelevanceDO.setPpid(long1);
//			
//			productRelevanceDao.save(productRelevanceDO);
//		}
//	}

	@Override
	@Transactional
	public int save(ProductDO product, List<Object> listproperty, List<Object> listprice, List<Object> listsize,
			List<Object> listcolor) {
		product.setCreatedate(new Date());
		product.setModifydate(new Date());
		int aa = productDao.save(product);
		if (!listproperty.isEmpty()) {
            for (Object obj : listproperty) {
                if (obj != null) {
                	ProductPropertyDO productPropertyDO = (ProductPropertyDO)obj;
                	if(StringUtils.isNoneBlank(productPropertyDO.getParameter())||StringUtils.isNoneBlank(productPropertyDO.getParametervalue())||productPropertyDO.getSort()!=null){
                		productPropertyDO.setPid(product.getId());
                		productPropertyDao.save(productPropertyDO);
                	}
                }
            }
        }
		if (!listprice.isEmpty()) {
            for (Object obj : listprice) {
                if (obj != null) {
                	ProductPriceDO productPriceDO = (ProductPriceDO)obj;
                	if(StringUtils.isNoneBlank(productPriceDO.getSpecification())||productPriceDO.getPrice()!=null||productPriceDO.getPromoteprice()!=null||StringUtils.isNoneBlank(productPriceDO.getStatus())||productPriceDO.getStatus()!=null){
                		productPriceDO.setPid(product.getId());
                		if(productPriceDO.getPromoteprice()==null){
                			productPriceDO.setPromoteprice(productPriceDO.getPrice());
                		}
                		productPriceDao.save(productPriceDO);
//                		if("2".equals(productPriceDO.getStatus())){
//                			product.setPpid(productPriceDO.getId());
//                			product.setPrice(productPriceDO.getPrice());
//                			product.setPromoteprice(productPriceDO.getPromoteprice());
//                			productDao.update(product);
//                		}
                	}
                }
            }
        }
		if (!listsize.isEmpty()) {
            for (Object obj : listsize) {
                if (obj != null) {
                	ProductSizeDO productSizeDO = (ProductSizeDO)obj;
                	if(StringUtils.isNoneBlank(productSizeDO.getMeasure())){
                		productSizeDO.setPid(product.getId());
                		productSizeDao.save(productSizeDO);
                	}
                }
            }
        }
		if (!listcolor.isEmpty()) {
            for (Object obj : listcolor) {
                if (obj != null) {
                	ProductColorDO productColorDO = (ProductColorDO)obj;
                	if(StringUtils.isNoneBlank(productColorDO.getColor())){
                		productColorDO.setPid(product.getId());
                		productColorDao.save(productColorDO);
                	}
                }
            }
        }
		return aa;
	}

	@Override
	@Transactional
	public void saveRelevance(Long pid, Long[] ppids) {
		productRelevanceDao.removeByProductid(pid);
		if(pid!=null&&ppids.length>0){
			for (Long string : ppids) {
				ProductRelevanceDO productRelevanceDO = new ProductRelevanceDO();
				productRelevanceDO.setPid(pid);
				productRelevanceDO.setPpid(string);
				productRelevanceDao.save(productRelevanceDO);
			}
		}
		
	}
	
}
